﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorldServer.DAO;
using WorldServer.Packets.Server;
using WorldServer.Data;
using System.Threading;
using WorldServer.Data.Templates;

namespace WorldServer.Models
{
    public class SkillEntry
    {
        public int SkillID;
        public short Level;
    }

    public class CharacterSkillList : EventAwareList<SkillEntry>
    {
        Character Owner;

        public CharacterSkillList(List<SkillEntry> Entries, Character owner)
            : base()
        {
            Owner = owner;
            AddRange(Entries);
            OnAdd += new EventAwareListAddEvent<SkillEntry>(CharacterSkillList_OnAdd);
            OnRemove += new EventAwareListRemoveEvent<SkillEntry>(CharacterSkillList_OnRemove);
        }

        void CharacterSkillList_OnRemove(object sender, EventArgs<SkillEntry> e)
        {
            SkillsDAO.DeleteSkill(Owner.GetGUID(), e.Value.SkillID);
            Owner.SendPacket(new SPkt_SkillList());
        }

        void CharacterSkillList_OnAdd(object sender, EventArgs<SkillEntry> e)
        {
            SkillsDAO.InsertSkill(Owner.GetGUID(), e.Value.SkillID, e.Value.Level);
            Owner.SendPacket(new SPkt_SkillList(e.Value.SkillID, e.Value.Level, 0));
        }

        public void UpdateSkillLevel(int skillId, short level)
        {
            SkillEntry entry = this.Find(e => e.SkillID == skillId);
            if (entry != null)
            {
                entry.Level = level;
                SkillsDAO.UpdateSkillLevel(Owner.GetGUID(), entry.SkillID, entry.Level);
            }
        }
    }

    public class CastedSkill
    {
        public AionObject Caster;
        public SkillTemplate SkTemplate;
        public byte SkLevel;
        public byte TargetType;
        public AionObject FirstTarget;
        public float x = 0, y = 0, z = 0;
        public ushort ClientTime;
        public Timer CastTimer;
        public bool CastComplete = false;

        public CastedSkill(AionObject caster, AionObject target, SkillTemplate template, byte level, byte targetType, float x, float y, float z, ushort ctime)
        {
            Caster = caster;
            FirstTarget = target;
            SkTemplate = template;
            SkLevel = level;
            TargetType = targetType;
            ClientTime = ctime;
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public void CancelCast()
        {
            if (!CastComplete)
            {
                CastTimer.Change(99999, 99999);
                CastTimer.Dispose();
                if (Caster is Character)
                {
                    Character c = Caster as Character;
                    c.SendClientMessage(AString.SKILL_CANCELED);
                    c.CurrentAction = null;
                }
                CastComplete = true;
            }
        }

        public void Pkt_Init()
        {
            Caster.NearbyObjects.BroadcastPacket(new SPkt_SkillInit(this), true);
        }

        public void Pkt_Use()
        {
            Caster.NearbyObjects.BroadcastPacket(new SPkt_SkillUse(this), true);
        }

    }

}
